package org.jetnuke.modules.admin.banners;

import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.BannerClientDAO;
import org.jetnuke.dao.BannerDAO;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.entities.Banner;
import org.jetnuke.entities.Bannerclient;

public class Banners implements IAction {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(Banners.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.common.IAction#exec(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse,
	 *      org.apache.velocity.VelocityContext, org.jetnuke.common.IModuleInfo,
	 *      java.lang.String, org.jetnuke.common.IUserInfo,
	 *      org.jetnuke.common.IAdminInfo)
	 */
	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - start"); //$NON-NLS-1$
		}

		BannerDAO bm = DataAccessDriver.getInst().newJBannerDAO();
		BannerClientDAO bcm = DataAccessDriver.getInst().newBannerClientDAO();
		List active = bm.selectAllActiveOrderTypeBID();

		// bannerPeer.doSelect(new
		// Criteria().add(bannerPeer.ACTIVE,1).addAscendingOrderByColumn(bannerPeer.TYPE).addAscendingOrderByColumn(bannerPeer.BID));
		List inactive = bm.selectAllInactiveOrderTypeBID();
		// bannerPeer.doSelect(new
		// Criteria().add(bannerPeer.ACTIVE,0).addAscendingOrderByColumn(bannerPeer.TYPE).addAscendingOrderByColumn(bannerPeer.BID));
		List clients = bcm.selectAll();// bannerclientPeer.doSelect(new
										// Criteria());
		Iterator clientIterator = clients.iterator();
		HashMap clientMap = new HashMap();
		while (clientIterator.hasNext()) {
			Bannerclient client = (Bannerclient) clientIterator.next();
			int iactive = bcm.countActiveForClient(client.getCID());
			// bannerPeer.count(new
			// Criteria().add(bannerPeer.CID,client.getCID()).add(bannerPeer.ACTIVE,1));
			int iinactive = bcm.countInactiveForClient(client.getCID());
			// bannerPeer.count(new
			// Criteria().add(bannerPeer.CID,client.getCID()).add(bannerPeer.ACTIVE,0));
			client.setActive(iactive);
			client.setInactive(iinactive);
			clientMap.put(new Integer(client.getCID()), client.getNAME());
		}
		Iterator activeIterator = active.iterator();
		while (activeIterator.hasNext()) {
			Banner ban = (Banner) activeIterator.next();
			ban
					.setClientName((String) clientMap.get(new Integer(ban
							.getCID())));
		}
		Iterator inactiveIterator = inactive.iterator();
		while (inactiveIterator.hasNext()) {
			Banner ban = (Banner) inactiveIterator.next();
			ban
					.setClientName((String) clientMap.get(new Integer(ban
							.getCID())));
		}
		vc.put("inactive", inactive);
		vc.put("active", active);
		vc.put("clients", clients);
		ActionForward returnActionForward = new ActionForward(op, info);
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
		}
		return returnActionForward;
	}

}
